/*
 * Copyright 2010 Pierre Heinzerling, Uwe Janner, Patrick Wright
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.upp.publinksky.dao;

import com.upp.publinksky.model.Link;
import com.upp.publinksky.model.Theme;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.Date;
import java.util.List;

/**
 */
@SuppressWarnings({"unchecked"})
public class LinkDAO {
    public static Link insertLink(final Theme theme, final String email, final String title, final String uriString, final PersistenceManager pm) {
        final Link link = new Link();
        link.setAddedByEmail(email);
        link.setAddedOn(new Date());
        link.setTheme(theme.getKey());
        link.setScore(theme.getMaxScore());
        link.setTitle(title);
        link.setUriString(uriString);
        pm.makePersistent(link);
        return link;
    }

    public static Link getLinkByThemeAndUri(final Theme theme, final String uriString, final PersistenceManager pm) {
        Query qry = pm.newQuery(Link.class);
        qry.setFilter("theme == thm && uriString == nu");
        qry.declareParameters("com.google.appengine.api.datastore.Key thm, String nu");

        List<Link> res = (List<Link>) qry.executeWithArray(theme.getKey(), uriString);
        return res.size() == 0 ? null : res.get(0);
    }

    public static List<Link> listLinksForTheme(final Theme theme, final PersistenceManager pm) {
        Query qry = pm.newQuery(Link.class);
        qry.setFilter("theme == thm");
        qry.declareParameters("com.google.appengine.api.datastore.Key thm");

        return (List<Link>) qry.execute(theme.getKey());
    }

    public static void restoreLink(final Link link, final PersistenceManager pm) {
        link.setScore(1);
        pm.makePersistent(link);
    }

    public static Link reduceLinkScore(final long linkId, final PersistenceManager pm) {
        Link link = pm.getObjectById(Link.class, linkId);
        link.reduceScore();
        pm.makePersistent(link);
        return link;
    }

    public static void incrementAllLinkScoresForTheme(final Theme theme, final PersistenceManager pm) {
        // increment score for all links
        List<Link> links = listLinksForTheme(theme, pm);
        for (Link link : links) {
            link.setScore(link.getScore() + 1);
            pm.makePersistent(link);
        }
    }

    public static void decrementAllLinkScoresForTheme(final Theme theme, final PersistenceManager pm) {
        // decrease score for all links
        List<Link> allLinks = listLinksForTheme(theme, pm);
        for (Link link : allLinks) {
            link.setScore(link.getScore() - 1);
            pm.makePersistent(link);
        }
    }
}
